//
//  YCWebViewController.m
//  iWeidao
//
//  Created by ShiXiong on 13-12-2.
//  Copyright (c) 2013年 Weidao. All rights reserved.
//

#import "YSWebViewController.h"

@interface YSWebViewController () {
    UIWebView *_webView;
}

@end

@implementation YSWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    //网页的title
    self.title = @"夜店骰王用户协议";
    
    UIImage *image = [UIImage imageNamed:@"back_btn_n"];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [button setImage:image forState:UIControlStateNormal];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self
               action:@selector(backBarButtonPressed)
     forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = barItem;
    
    //加载网页内容
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x,
                                                           self.view.frame.origin.y,
                                                           self.view.frame.size.height,
                                                           self.view.frame.size.width)];
    _webView.backgroundColor = [UIColor whiteColor];
    NSURL *url = [NSURL URLWithString:@"http://www.yedianshaiwang.com/html5/memberRule.html"];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    _webView.delegate = self;
    [self.view addSubview:_webView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


- (void)backBarButtonPressed {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Setting Method
- (void)setTitleString:(NSString *)titleString
{
    _titleString = titleString;
    self.title = titleString;
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([request.URL.absoluteString rangeOfString:@"itunes.apple.com/cn/app"].location != NSNotFound) {
        NSURL *url = [NSURL URLWithString:_urlString];
        NSURLRequest * request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];
    }
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    if (error.code == 102 || error.code == -999) {
        return;
    }
}

@end
